import React, { useState } from 'react';

import { CheckboxOptionType, Radio, RadioGroupProps } from 'antd';

export default function<T>(
  initValue: RadioGroupProps['value'],
  options: CheckboxOptionType[],
): {
  value: T | any;
  setValue: (e: CheckboxOptionType | undefined) => void;
  DOM: (e: RadioGroupProps) => JSX.Element;
} {
  const [value, setValue] = useState<T | any>(
    options?.find((item: any) => initValue === item?.value) || {
      label: '',
      value: initValue,
    },
  );
  const DOM = ({ ...e }: RadioGroupProps): JSX.Element => (
    <Radio.Group
      optionType="button"
      buttonStyle="solid"
      {...e}
      options={options}
      onChange={e =>
        setValue(
          options?.find((item: any) => e.target.value === item?.value) || {
            label: '',
            value: initValue,
          },
        )
      }
      defaultValue={value?.value}
    />
  );
  return {
    value,
    setValue,
    DOM,
  };
}
